<!--
 * @Description: 附加项目新增
 * @Autor: 何兴家
 * @Date: 2020-12-14 14:07:19
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-05-12 10:19:43
-->

<template>
  <div>
    <FormPanel ref="form" :list="formList" :cols="1" :labelWidth="110" :isSubmitBtn="false"></FormPanel>

    <div class="dialogBottmView">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';
import { queryRoAddItem } from '@/api/repair/repairReception/customer';

export default {
  props: ['chargePartitionDict', 'defaultChargePartition'],
  mixins: [dictionary],
  data() {
    this.addItemList = []; // 所有的附加项目
    return {
      baseAddItemAmount: 0, // 基础附加项目费用（判断是否修改）
      formList: this.createFormList()
    };
  },
  computed: {
    isChangePrice() {
      return !(this.baseAddItemAmount == findFormItem(this.formList, 'ADD_ITEM_AMOUNT').initialValue);
    }
  },
  watch: {
    isChangePrice(newVal, oldVal) {
      findFormItem(this.formList, 'REMARK').rules = newVal ? [{ required: true, message: '附加项目费用有变动，备注不能为空', trigger: 'blur' }] : [];
      !newVal && this.$refs.form.CLEAR_VALIDATE();
    }
  },
  mounted() {
    this.initDefaultParams();
    this.qeuryAdditemList();
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '附加项目名称',
          fieldName: 'ADD_ITEM_NAME',
          itemList: [],
          initialValue: '',
          change: val => {
            this.handleAddItemChange(val);
          }
        },
        {
          type: 'INPUT',
          label: '附加项目代码',
          fieldName: 'ADD_ITEM_CODE',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '附加项目费用',
          fieldName: 'ADD_ITEM_AMOUNT',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '收费区分代码',
          fieldName: 'CHARGE_PARTITION_CODE',
          initialValue: '',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '备注',
          initialValue: '',
          rules: [],
          fieldName: 'REMARK'
        }
      ];
    },
    // 初始化数据字典及默认参数
    initDefaultParams() {
      findFormItem(this.formList, 'CHARGE_PARTITION_CODE').itemList = this.chargePartitionDict;
      findFormItem(this.formList, 'CHARGE_PARTITION_CODE').initialValue = this.defaultChargePartition || 'Z';
    },
    // 查询附加项目列表
    async qeuryAdditemList() {
      let res = await queryRoAddItem();
      if (res.resultCode == 200) {
        findFormItem(this.formList, 'ADD_ITEM_NAME').itemList = res.data.map(item => ({ text: item.ADD_ITEM_NAME, value: item.ADD_ITEM_NAME }));
        this.addItemList = res.data;
      }
    },
    // 附加项目变化处理函数
    handleAddItemChange(val) {
      let selectedAddItem = this.addItemList.find(item => item.ADD_ITEM_NAME == val);
      findFormItem(this.formList, 'ADD_ITEM_CODE').initialValue = selectedAddItem.ADD_ITEM_CODE;
      findFormItem(this.formList, 'ADD_ITEM_AMOUNT').initialValue = selectedAddItem.ADD_ITEM_AMOUNT;
      this.baseAddItemAmount = selectedAddItem.ADD_ITEM_AMOUNT;
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    },
    // 点击确定
    async handleConfirm() {
      let res = await this.$refs.form.GET_FORM_DATA();
      console.log(res);
      if (res[0]) return;
      this.$emit('onConfirm', res[1]);
    }
  }
};
</script>

<style scoped></style>
